package com.asset.modules.common.handler;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author chenchen
 */
public class AssetSheetWriteHandler implements SheetWriteHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(AssetSheetWriteHandler.class);

    private String title;

    private String companyName;

    private String date;


    public AssetSheetWriteHandler(String title, String companyName, String date){
        this.title = title;
        this.companyName = companyName;
        this.date = date;
    }

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

    }


    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        LOGGER.info("第{}个sheet写入成功!", writeSheetHolder.getSheetNo());
        Workbook workbook = writeWorkbookHolder.getWorkbook();
        Sheet sheet = workbook.getSheetAt(0);
        //设置主标题
        Row row2 = sheet.createRow(0);
        //设置公司名称
        Row row3 = sheet.createRow(1);
        row2.setHeight((short) 600);
        row3.setHeight((short) 600);
        Cell cell1 = row2.createCell(0);
        Cell cell2 = row3.createCell(0);
        cell1.setCellValue(this.title);
        cell2.setCellValue(this.companyName);
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        Font font = workbook.createFont();
        font.setBold(true);
        font.setFontHeight((short) 400);
        cellStyle.setFont(font);
        cell1.setCellStyle(cellStyle);
        cell2.setCellStyle(cellStyle);

        //设置截止日期
        CellStyle cellStyle1 = workbook.createCellStyle();
        cellStyle1.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle1.setAlignment(HorizontalAlignment.RIGHT);
        Row row1 = sheet.createRow(2);
        row1.setHeight((short) 500);
        Cell cell = row1.createCell(0);
        //设置单元格风格
        cell.setCellStyle(cellStyle1);
        //设置单元格内容
        cell.setCellValue("截止日期：" + this.date);

        sheet.addMergedRegionUnsafe(new CellRangeAddress(1, 1, 0, 17));
        sheet.addMergedRegionUnsafe(new CellRangeAddress(0, 0, 0, 17));
        sheet.addMergedRegionUnsafe(new CellRangeAddress(2, 2, 0, 17));

    }
}
